package controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import serivce.MapService;
import test.test;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.ResourceBundle;
import java.util.UUID;

@Controller
public class Test {

    @Autowired
    private MapService mapService;
    @RequestMapping("/test")
    public void test(String account,String password){

    }


    @RequestMapping(value = "/dotest",method = RequestMethod.POST)
    @ResponseBody
    public String dotest(@RequestBody String data){
//        System.out.println(request.getHeaderNames());
//        Enumeration<String> headNames= request.getHeaderNames();
//        while (headNames.hasMoreElements()){
//            String headnaem= headNames.nextElement();
//            System.out.println(request.getHeader(headnaem));
//        }
        System.out.println("account"+data);
        System.out.println("passowrd"+data);
        return data;
    }
    @ResponseBody
    @RequestMapping("/map")
    public JSONObject map(HttpServletRequest request,@Param("ip") String ip){
        System.out.println("ip="+ip);
        mapService.getAddress(ip);

        return null;
    }

    @RequestMapping("/area")
    @ResponseBody
    public JSONArray areaData(HttpServletRequest request){
        String path=request.getSession().getServletContext().getRealPath("/");
        System.out.println("path="+path);
        File f=new File(path+"statics/area2.json");
        InputStream in=null;
        System.out.println("文件大小"+f.length());
        StringBuilder str=null;
        int length=1024;
        long num=f.length();
        for(length=1024;length<num;length++){
            if (num%length==0){
                break;
            }
        }
        try {
            str=new StringBuilder();
           in =new FileInputStream(f);
           byte[] b=new byte[length];

           while ((in.read(b))!=-1){
               str.append(new String(b,"UTF-8"));
           }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (in!=null)
                {
                    in.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        String s=new String(str);

        JSONArray jsonArray=JSON.parseArray(s);
//        JSONObject json= JSONArray.parseObject(s);
        return jsonArray;
    }


    @RequestMapping("/pay")
    public String pay(){
        com.alipay.easysdk.factory.Factory.setOptions(setOPtion());
        AlipayTradePagePayResponse response=null;
        try {
            String id= UUID.randomUUID().toString();
            String returnurl="http";
            response= Factory.Payment.Page().pay("测试",id,"12",returnurl);

            // 3. 处理响应或异常
            if (ResponseChecker.success(response)) {
                System.out.println(response);
                System.out.println(response.getBody());

                System.out.println("调用成功");
            } else {
                System.err.println("调用失败，原因：" + response.toString());
            }
        } catch (Exception e) {
            System.err.println("调用遭遇异常，原因：" + e.getMessage());
            throw new RuntimeException(e.getMessage(), e);
        }
        return  response.body;
    }

    private static Config setOPtion(){
        ResourceBundle res= ResourceBundle.getBundle("conf/pay");
        Config f=new Config();
        f.alipayPublicKey= res.getString("alipayPublicKey");
        f.appId=res.getString("appid");
        f.merchantPrivateKey=res.getString("privateKey");
        f.protocol = "https";
        f.gatewayHost = "openapi.alipaydev.com";
        f.signType = "RSA2";
        System.out.println(f.appId);
        System.out.println(f);
        return f;
    }
}
